package cn.tedu.vrd07.controller;

import cn.tedu.vrd07.entity.Category;
import cn.tedu.vrd07.mapper.CategoryMapper;
import cn.tedu.vrd07.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/category/")
public class CategoryController {
    @Autowired(required = false)
    CategoryMapper mapper;
    @Autowired(required = false)
    ProductMapper productMapper;

    @Value("${dirPath}")
    private String dirPath;

    @RequestMapping("select")
    public List<Category> select(){
        return mapper.select();
    }

    @RequestMapping("delete")
    public void delete(int id){
        mapper.delete(id);
    }

    @RequestMapping("insert")
    public void insert(Category category){
        mapper.insert(category);
    }

    @RequestMapping("deleteAll")
    public String deleteAll(int id){
        List<String> urrArr = productMapper.selectUrlByCid(id);
        for(String url : urrArr){
            String filePath = dirPath+url;
            new File(filePath).delete();
        }

        int row = productMapper.deleteByCid(id);

        mapper.delete(id);

        return "删除了分类和"+row+"个作品!";

    }
}
